data "alicloud_zones" main {
  available_resource_creation    = "VSwitch"
}

data "alicloud_instance_types" "main" {
    # availability_zone            = data.alicloud_zones.main.zones.0.id
    availability_zone            = var.ack_instance_availability_zone != "" ? var.ack_instance_availability_zone : data.alicloud_zones.main.zones.0.id
    cpu_core_count               = var.ack_instance_cpu_core_count
    memory_size                  = var.ack_instance_memory_size
    instance_type_family         = var.ack_instance_type_family
    kubernetes_node_role         = "Worker"
}

resource "alicloud_cs_autoscaling_config" "main" {
  cluster_id                = var.ack_cluster_id
  // configure auto scaling
  cool_down_duration        = "10m"
  unneeded_duration         = "10m"
  utilization_threshold     = "0.5"
  gpu_utilization_threshold = "0.5"
  scan_interval             = "60s"
  scale_down_enabled        = var.scale_down_enabled
  expander                  = "least-waste"
}

resource "alicloud_cs_kubernetes_node_pool" "main" {
    name                          = "${var.ack_cluster_name}-${var.ack_node_pool_name}"
    cluster_id                    = var.ack_cluster_id
    vswitch_ids                   = length(var.ack_vswitch_ids) > 0 ? split(",", join(",", var.ack_vswitch_ids)): length(var.ack_vswitch_ids) < 1 ? [] : []
    instance_types                = [data.alicloud_instance_types.main.instance_types.0.id]
    security_group_ids            = var.ack_security_group_ids
    system_disk_category          = var.ack_system_disk_category
    system_disk_size              = var.ack_system_disk_size
    system_disk_performance_level = var.ack_system_disk_performance_level 
    system_disk_encrypted         = false
    dynamic "data_disks" {
        for_each = var.ack_data_disks
        content {
        size                   = lookup(data_disks.value, "ack_data_size", null)
        category               = lookup(data_disks.value, "ack_data_category", null)
        encrypted              = lookup(data_disks.value, "ack_data_encrypted", null)
        performance_level      = lookup(data_disks.value, "ack_data_performance_level", null)
        kms_key_id             = lookup(data_disks.value, "ack_data_kms_key_id", null)
        }
    }     
    key_name                      = var.ack_key_name
    image_id                      = var.ack_image_id
    image_type                    = "AliyunLinux"
    soc_enabled                   = var.ack_soc_enabled
    spot_strategy                 = var.ack_spot_strategy
    scaling_policy                = "release"
    rds_instances                 = []
    polardb_ids                   = []

    runtime_name                  = var.ack_runtime_name
    runtime_version               = var.ack_runtime_version

    dynamic "labels" {
        for_each = var.ack_cluster_labels
        content {
            key     = lookup(labels.value, "key", var.ack_cluster_labels)
            value   = lookup(labels.value, "value", var.ack_cluster_labels)
        }
    }   
    dynamic "taints" {
        for_each = var.ack_cluster_taints
        content {
            key     = lookup(taints.value, "key", var.ack_cluster_taints)
            value   = lookup(taints.value, "value", var.ack_cluster_taints)
            effect  = lookup(taints.value, "effect", var.ack_cluster_taints)
        }
    } 
    dynamic "scaling_config" {
        for_each = var.ack_scaling_config != null ? var.ack_scaling_config : []
        content {
            min_size                    = lookup(scaling_config.value, "min_size", null)
            max_size                    = lookup(scaling_config.value, "max_size", null)
            type                        = lookup(scaling_config.value, "type", null)
            is_bond_eip                 = lookup(scaling_config.value, "is_bond_eip", null)
            eip_internet_charge_type    = lookup(scaling_config.value, "eip_internet_charge_type", null)
            eip_bandwidth               = lookup(scaling_config.value, "eip_bandwidth", null)            
        }
    }   

    dynamic "management" {
        for_each = var.ack_nodepool_config_management
        content {
            auto_repair         = lookup(management.value, "auto_repair", null)
            auto_upgrade        = lookup(management.value, "auto_upgrade", null)
            surge               = lookup(management.value, "surge", null)
            max_unavailable     = lookup(management.value, "max_unavailable", null)
            surge_percentage    = lookup(management.value, "surge_percentage", null)
        }
    } 
    dynamic "kubelet_configuration" {
        for_each = var.ack_kubelet_configuration
        content {
            registry_pull_qps       = lookup(kubelet_configuration.value, "registry_pull_qps", null)
            registry_burst          = lookup(kubelet_configuration.value, "registry_burst", null)
            event_record_qps        = lookup(kubelet_configuration.value, "event_record_qps", null)
            event_burst             = lookup(kubelet_configuration.value, "event_burst", null)

            # eviction_hard = {
            #     memory.available                = ""
            #     nodefs.available                = ""
            #     nodefs.inodesFree               = ""
            #     imagefs.available               = ""

            #     imagefs.inodesFree              = ""
            #     allocatableMemory.available     = ""
            #     pid.available                   = ""          
            # }

            system_reserved = {
                cpu                   = lookup(kubelet_configuration.value, "system_reserved_cpu", null)
                memory                = lookup(kubelet_configuration.value, "system_reserved_memory", null)
                ephemeral-storage     = "10Gi" 
            }

            kube_reserved = {
                cpu                   = lookup(kubelet_configuration.value, "kube_reserved_cpu", null)
                memory                = lookup(kubelet_configuration.value, "kube_reserved_memory", null)
            } 
        }               
    }      
    depends_on                    = [
        var.ack_cluster_id,
        alicloud_cs_autoscaling_config.main    
    ]                           
    resource_group_id             = var.resource_group_id  
    tags                          = var.tags
}